package com.wy.allthingdone.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.widget.ImageView;

import com.wy.allthingdone.R;

/**
 * Created by 38270 on 2016/11/21.
 * 圆形边框图片 或者圆角
 */

public class ByCircleImageView extends ImageView {
    private static final int BORDER_RADUIS_DEFAILT = 30;
    private static final int TYPE_CIRCLE = 0;
    private static final int TYPE_ROUND = 1;
    private Paint paint;
    private int borderRadius;
    private int type;
    private int width;
    private int radius;
    private Matrix matrix;
    private RectF rectF;

    public ByCircleImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public ByCircleImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        inital(context, attrs);
    }

    public ByCircleImageView(Context context) {
        super(context);

    }

    private void inital(Context context, AttributeSet attrs) {
        matrix = new Matrix();
        paint = new Paint();
        paint.setAntiAlias(true);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.RoundAngerImageView);
        float diydp = array.getDimension(R.styleable.RoundAngerImageView_borderRadius, 30);
        Log.i("TAG", "diydp==============" + diydp);
        borderRadius = dp2px((int) diydp);
        Log.i("TAG", "borderRadius==============" + borderRadius);
        type = array.getInt(R.styleable.RoundAngerImageView_type, TYPE_CIRCLE);
        array.recycle();
    }

    //dp2px
    public int dp2px(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (type == TYPE_CIRCLE) {
            width = Math.min(getMeasuredWidth(), getMeasuredHeight());
            radius = width / 2;
            setMeasuredDimension(width, width);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (getDrawable() == null) {
            return;
        }
        setImageShader();
        if (type == TYPE_CIRCLE) {
            canvas.drawCircle(radius, radius, radius, paint);
        } else {
            canvas.drawRoundRect(rectF, borderRadius, borderRadius, paint);
        }
//        super.onDraw(canvas);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (type == TYPE_ROUND) {
            rectF = new RectF(0, 0, getWidth(), getHeight());
        }
    }

    private void setImageShader() {
        Bitmap bitmap = ((BitmapDrawable) getDrawable()).getBitmap();
        BitmapShader bitmapshder = new BitmapShader(bitmap, Shader.TileMode.MIRROR, Shader.TileMode.MIRROR);
        float scale = 1.0f;
        int viewwidth = getWidth();
        int viewheight = getHeight();
        int drawablewidth = bitmap.getWidth();
        int drawableheight = bitmap.getHeight();
        float dx = 0, dy = 0;
        if (type == TYPE_CIRCLE) {
            //
            int size = Math.min(bitmap.getWidth(), bitmap.getHeight());
            scale = width * 1.0f / size;
        } else if (type == TYPE_ROUND) {
            scale = Math.min(getWidth() * 1.0f / bitmap.getWidth(), getHeight() * 1.0f / bitmap.getHeight());
        }
        if (drawablewidth * viewwidth > viewheight * drawableheight) {
            dx = (viewwidth - drawablewidth * scale) * 0.5f;
        } else {
            dy = (viewheight - drawableheight * scale) * 0.5f;
        }

        matrix.setScale(scale, scale);
        matrix.postTranslate((int) (dx + 0.5f), (int) (dy + 0.5f));

        bitmapshder.setLocalMatrix(matrix);
        paint.setShader(bitmapshder);
    }
}
